<?php
/**
 * 默认接口服务类
 *
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */

class Api_Default extends PhalApi_Api {

	public function getRules() {
        return array(
            'index' => array(
                'username' 	=> array('name' => 'username', 'default' => 'PHPer', ),
            ),
            'express' => array(
                'order' => array('name' => 'order', 'type' => 'string', 'require' => true, 'desc' => '快递单号' ),
                'key' => array('name' => 'key', 'type' => 'string', 'require' => false, 'desc' => '身份标识' ),
            ),
            'barCode' => array(
                'ean' => array('name' => 'ean', 'type' => 'string', 'require' => true, 'desc' => '条码号' ),
                'key' => array('name' => 'key', 'type' => 'string', 'require' => false, 'desc' => '身份标识' ),
            )
        );
	}
	
	/**
	 * 默认接口服务
	 * @return string title 标题
	 * @return string content 内容
	 * @return string version 版本，格式：X.X.X
	 * @return int time 当前时间戳
	 */
	public function index() {
        return array(
            'title' => 'Hello World!',
            'content' => T('DE_WELCOME', [$this->username]),
            'version' => PHALAPI_VERSION,
            'time' => $_SERVER['REQUEST_TIME'],
        );
	}

    /**
     * 快递接口
     * @return int code 操作码
     * @return string msg 提示信息，只有错误时候才有值
     * @return Object info 接口数据对象
     * @return string info.order 订单号
     * @return string info.isCheck 是否已被签收
     * @return string info.companyName 快递公司名称
     * @return Object info.details 具体物流信息数据对象
     * @return string info.details.time 物流信息提交时间
     * @return string info.details.context 具体物流内容
     * @return string info.details.ftime 无意义
     * @return string info.thanks 特别鸣谢|接口数据源
     */
    public function express(){
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $domain = new Domain_Default();
        $info = $domain->getExpressInfo($this->order);
        if( isset($info['status']) ){
            if ( $info['status'] == ReturnCode::NOT_EXISTS ) {
                $rs['code'] = ReturnCode::NOT_EXISTS;
                $rs['msg'] = T('NOT_EXISTS',[$this->order]);
                return $rs;
            }

            if ( $info['status'] == 400 ) {
                $rs['code'] = ReturnCode::PARAMS_ERROR;
                $rs['msg'] = T('PARAMS_ERROR');
                return $rs;
            }

            if ( $info['status'] == 201 ) {
                $rs['code'] = ReturnCode::PARAMS_ERROR;
                $rs['msg'] = $info['message'];
                return $rs;
            }
        }

        $rs['info'] = $info;

        return $rs;
    }

    /**
     * 条形码接口
     * @return int code 操作码
     * @return string msg 提示信息，只有错误时候才有值
     * @return Object info 接口数据对象
     * @return string info.ean 条码
     * @return string info.titleSrc 商品名图片链接
     * @return string info.guobie 商品所属国籍
     * @return string info.price 商品价格
     * @return string info.supplier 商品所属公司
     * @return string info.sort_id 未知
     * @return string info.faccode 厂商代码
     * @return string info.fac_name 厂商名称
     * @return string info.fac_status 条码状态
     * @return string info.thanks 特别鸣谢|接口数据源
     */
    public function barCode(){
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $domain = new Domain_Default();
        $info = $domain->getBarCodeInfo($this->ean);
        $rs['info'] = $info;

        return $rs;
    }

}
